﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;

namespace GUIL
{
    public partial class MovieGUI : Form, RemoteService.IRemoteServiceCallback
    {
        RemoteService.IRemoteService _remote;
        readonly RemoteService.Cinema _selectedCinema;

        public MovieGUI(RemoteService.Cinema cinema)
        {
            // Hosting client interface ... (more info later)
            // Connection hasn't been opened yet!
            _remote = new RemoteService.RemoteServiceClient(new InstanceContext(this), "WSDualHttpBinding_IRemoteService");
            
            _selectedCinema = cinema;
            
            InitializeComponent();

            // Activating key listener
            this.textBoxMovieTitle.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckEnter);
        }

        /// <summary>
        /// Do something on enter press
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckEnter(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            // 13 = enter/return
            if (e.KeyChar == (char)13 && !String.IsNullOrWhiteSpace(textBoxMovieTitle.Text))
            {
                // Try to access remote and get search results from IMDB
                try
                {
                    dataGridViewMovieList.DataSource = _remote.SearchIMDBMovies(textBoxMovieTitle.Text);
                }
                catch (Exception)
                {
                    MessageBox.Show("API is down!");
                }
            }
        }

        private void buttonSaveMovie_Click(object sender, EventArgs e)
        {
            if (dataGridViewMovieList.CurrentRow != null)
            {
                try
                {
                    // Save movie to db with cinema added
                    RemoteService.Movie currentMovie = dataGridViewMovieList.CurrentRow.DataBoundItem as RemoteService.Movie;
                    currentMovie.Cinemas.Add(_selectedCinema);
                    _remote.AddMovie(currentMovie);

                    DialogResult dialogResult = MessageBox.Show("Do you want to add screencast to the movie?", "Movie saved", MessageBoxButtons.YesNo);

                    if (dialogResult == DialogResult.Yes)
                    {
                        // open screen cast gui
                        using (Form form = new ScreenCastGUI(currentMovie, _selectedCinema))
                        {
                            form.ShowDialog(); // open gui, without catching result
                        }
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                        MessageBox.Show("Movie saved");
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Movie failed to save");
                }
            }
            else
            {
                MessageBox.Show("Select a movie!");
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            // Search by btn press
            if (!String.IsNullOrWhiteSpace(textBoxMovieTitle.Text))
            {
                try
                {
                    dataGridViewMovieList.DataSource = _remote.SearchIMDBMovies(textBoxMovieTitle.Text);
                }
                catch (Exception)
                {
                    MessageBox.Show("API is down!");
                }
            }
        }

        private void dataGridViewMovieList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // Add actors, related to selected movie, to dataGrid (To let the user edit actors if needed)
            dataGridViewActors.DataSource = (dataGridViewMovieList.CurrentRow.DataBoundItem as RemoteService.Movie).Actors;
        }


        public void OnAction(List<RemoteService.ScreenCast_Seat> screenSeats)
        {
            throw new NotImplementedException();
        }
    }
}
